home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / cmds.fmt / nawk.man < prev    next >
Encoding:
Text File  |  1989-01-05  |  11.5 KB  |  331 lines

  1.  
  2.  
  3.  
  4. AWK                       User Commands                       AWK
  5.  
  6.  
  7.  
  8. NNAAMMEE
  9.      awk - pattern-directed scanning and processing language
  10.  
  11. SSYYNNOOPPSSIISS
  12.      aawwkk [ --FF_f_s ] [ prog ] [ file ... ]
  13.  
  14. DDEESSCCRRIIPPTTIIOONN
  15.      _A_w_k scans each input _f_i_l_e for lines that match any of a set
  16.      of patterns specified literally in _p_r_o_g or in a file speci-
  17.      fied as --ff _f_i_l_e.  With each pattern there can be an associ-
  18.      ated action that will be performed when a line of a _f_i_l_e
  19.      matches the pattern.  Each line is matched against the pat-
  20.      tern portion of every pattern-action statement; the associ-
  21.      ated action is performed for each matched pattern.  The file
  22.      name means the standard input.  Any _f_i_l_e of the form
  23.      _v_a_r=_v_a_l_u_e is treated as an assignment, not a filename.
  24.  
  25.      An input line is made up of fields separated by white space,
  26.      or by regular expression FFSS.  The fields are denoted $$11, $$22,
  27.      ...; $$00 refers to the entire line.
  28.  
  29.      A pattern-action statement has the form
  30.  
  31.           _p_a_t_t_e_r_n {{ _a_c_t_i_o_n }}
  32.  
  33.      A missing {{ _a_c_t_i_o_n }} means print the line; a missing pattern
  34.      always matches.  Pattern-action statements are separated by
  35.      newlines or semicolons.
  36.  
  37.      An action is a sequence of statements.  A statement can be
  38.      one of the following:
  39.  
  40.           iiff(( _e_x_p_r_e_s_s_i_o_n )) _s_t_a_t_e_m_e_n_t [ eellssee _s_t_a_t_e_m_e_n_t ] wwhhiillee((
  41.           _e_x_p_r_e_s_s_i_o_n )) _s_t_a_t_e_m_e_n_t ffoorr(( _e_x_p_r_e_s_s_i_o_n ;; _e_x_p_r_e_s_s_i_o_n ;;
  42.           _e_x_p_r_e_s_s_i_o_n )) _s_t_a_t_e_m_e_n_t ffoorr(( _v_a_r iinn _a_r_r_a_y )) _s_t_a_t_e_m_e_n_t ddoo
  43.           _s_t_a_t_e_m_e_n_t wwhhiillee(( _e_x_p_r_e_s_s_i_o_n )) bbrreeaakk ccoonnttiinnuuee {{ [ _s_t_a_t_e_-
  44.           _m_e_n_t ... ] }} _e_x_p_r_e_s_s_i_o_n                ## commonly _v_a_r =
  45.           _e_x_p_r_e_s_s_i_o_n pprriinntt [ _e_x_p_r_e_s_s_i_o_n-_l_i_s_t ] [ >> _e_x_p_r_e_s_s_i_o_n ]
  46.           pprriinnttff _f_o_r_m_a_t [ ,, _e_x_p_r_e_s_s_i_o_n-_l_i_s_t ] [ >> _e_x_p_r_e_s_s_i_o_n ]
  47.           rreettuurrnn [ _e_x_p_r_e_s_s_i_o_n ] nneexxtt                      ## skip
  48.           remaining patterns on this input line ddeelleettee _a_r_r_a_y[[
  49.           _e_x_p_r_e_s_s_i_o_n ]]## delete an array element eexxiitt [ _e_x_p_r_e_s_s_i_o_n
  50.           ]       ## exit immediately; status is _e_x_p_r_e_s_s_i_o_n
  51.  
  52.      Statements are terminated by semicolons, newlines or right
  53.      braces.  An empty _e_x_p_r_e_s_s_i_o_n-_l_i_s_t stands for $$00.  String
  54.      constants are quoted " ", with the usual C escapes recog-
  55.      nized within.  Expressions take on string or numeric values
  56.      as appropriate, and are built using the operators ++ -- ** // %%
  57.      ^^ (exponentiation), and concatenation (indicated by a
  58.      blank).  The operators ++++ ---- ++== --== **== //== %%== ^^== ****== >> >>== << <<==
  59.      ==== !!== ??:: are also available in expressions.  Variables may
  60.  
  61.  
  62.  
  63. AT&T UNIX System Toolchest7 December 1987                        1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. AWK                       User Commands                       AWK
  71.  
  72.  
  73.  
  74.      be scalars, array elements (denoted _x[[_i]]) or fields.  Vari-
  75.      ables are initialized to the null string.  Array subscripts
  76.      may be any string, not necessarily numeric; this allows for
  77.      a form of associative memory.  Multiple subscripts such as
  78.      [[ii,,jj,,kk]] are permitted; the constituents are concatenated,
  79.      separated by the value of SSUUBBSSEEPP.
  80.  
  81.      The pprriinntt statement prints its arguments on the standard
  82.      output (or on a file if >>_f_i_l_e or >>>>_f_i_l_e is present or on a
  83.      pipe if ||_c_m_d is present), separated by the current output
  84.      field separator, and terminated by the output record separa-
  85.      tor.  _f_i_l_e and _c_m_d may be literal names or parenthesized
  86.      expressions; identical string values in different statements
  87.      denote the same open file.  The pprriinnttff statement formats its
  88.      expression list according to the format (see _p_r_i_n_t_f(3)).
  89.      The built-in function cclloossee((_e_x_p_r)) closes the file or pipe
  90.      _e_x_p_r.
  91.  
  92.      The customary functions eexxpp, lloogg, ssqqrrtt, ssiinn, ccooss, aattaann22 are
  93.      built in.  Other built-in functions:
  94.  
  95.      lleennggtthh
  96.           the length of its argument taken as a string, or of $$00
  97.           if no argument.
  98.  
  99.      rraanndd random number on (0,1)
  100.  
  101.      ssrraanndd
  102.           sets seed for rraanndd
  103.  
  104.      iinntt  truncates to an integer value
  105.  
  106.      ssuubbssttrr((_s,, _m,, _n))
  107.           the _n-character substring of _s that begins at position
  108.           _m counted from 1.
  109.  
  110.      iinnddeexx((_s,, _t))
  111.           the position in _s where the string _t occurs, or 0 if it
  112.           does not.
  113.  
  114.      mmaattcchh((_s,, _r))
  115.           the position in _s where the regular expression _r
  116.           occurs, or 0 if it does not.  The variables RRSSTTAARRTT and
  117.           RRLLEENNGGTTHH are set to the position and length of the
  118.           matched string.
  119.  
  120.      sspplliitt((_s,, _a,, _f_s))
  121.           splits the string _s into array elements _a[[11]], _a[[22]],
  122.           ..., _a[[_n]], and returns _n.  The separation is done with
  123.           the regular expression _f_s or with the field separator
  124.           FFSS if _f_s is not given.
  125.  
  126.  
  127.  
  128.  
  129. AT&T UNIX System Toolchest7 December 1987                        2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. AWK                       User Commands                       AWK
  137.  
  138.  
  139.  
  140.      ssuubb((_r,, _t,, _s))
  141.           substitutes _t for the first occurrence of the regular
  142.           expression _r in the string _s.  If _s is not given, $$00 is
  143.           used.
  144.  
  145.      ggssuubb same as ssuubb except that all occurrences of the regular
  146.           expression are replaced; ssuubb and ggssuubb return the number
  147.           of replacements.
  148.  
  149.      sspprriinnttff((_f_m_t,, _e_x_p_r,, ... ))
  150.           the string resulting from formatting _e_x_p_r ... according
  151.           to the _p_r_i_n_t_f(3) format _f_m_t
  152.  
  153.      ssyysstteemm((_c_m_d))
  154.           executes _c_m_d and returns its exit status
  155.  
  156.      The ``function'' ggeettlliinnee sets $$00 ttoo the next input record
  157.      from the current input file; ggeettlliinnee <<_f_i_l_e sets $$00 to the
  158.      next record from _f_i_l_e.  ggeettlliinnee _x sets variable _x instead.
  159.      Finally, _c_m_d || ggeettlliinnee pipes the output of _c_m_d into ggeettlliinnee;
  160.      each call of ggeettlliinnee returns the next line of output from
  161.      _c_m_d.  In all cases, ggeettlliinnee returns 1 for a successful
  162.      input, 0 for end of file, and -1 for an error.
  163.  
  164.      Patterns are arbitrary Boolean combinations (with !! |||| &&&&)
  165.      of regular expressions and relational expressions.  Regular
  166.      expressions are as in _e_g_r_e_p(1).  Isolated regular expres-
  167.      sions in a pattern apply to the entire line.  Regular
  168.      expressions may also occur in relational expressions, using
  169.      the operators ~~ and !!~~.  //_r_e// is a constant regular expres-
  170.      sion; any string (constant or variable) may be used as a
  171.      regular expression, except in the position of an isolated
  172.      regular expression in a pattern.
  173.  
  174.      A pattern may consist of two patterns separated by a comma;
  175.      in this case, the action is performed for all lines from an
  176.      occurrence of the first pattern though an occurrence of the
  177.      second.
  178.  
  179.      A relational expression is one of the following:
  180.  
  181.           _e_x_p_r_e_s_s_i_o_n _m_a_t_c_h_o_p _r_e_g_u_l_a_r-_e_x_p_r_e_s_s_i_o_n
  182.           _e_x_p_r_e_s_s_i_o_n _r_e_l_o_p _e_x_p_r_e_s_s_i_o_n
  183.  
  184.      where a relop is any of the six relational operators in C,
  185.      and a matchop is either ~~ (matches) or !!~~ (does not match).
  186.      A conditional is an arithmetic expression, a relational
  187.      expression, or a Boolean combination of these.
  188.  
  189.      The special patterns BBEEGGIINN and EENNDD may be used to capture
  190.      control before the first input line is read and after the
  191.      last.  BBEEGGIINN and EENNDD do not combine with other patterns.
  192.  
  193.  
  194.  
  195. AT&T UNIX System Toolchest7 December 1987                        3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. AWK                       User Commands                       AWK
  203.  
  204.  
  205.  
  206.      Variable names with special meanings:
  207.  
  208.      FFSS   regular expression used to separate fields; also sett-
  209.           able by option --FF_f_s.
  210.  
  211.      NNFF   number of fields in the current record
  212.  
  213.      NNRR   ordinal number of the current record
  214.  
  215.      FFNNRR  ordinal number of the current record in the current
  216.           file
  217.  
  218.      FFIILLEENNAAMMEE
  219.           the name of the current input file
  220.  
  221.      RRSS   input record separator (default newline)
  222.  
  223.      OOFFSS  output field separator (default blank)
  224.  
  225.      OORRSS  output record separator (default newline)
  226.  
  227.      OOFFMMTT output format for numbers (default %%..66gg)
  228.  
  229.      SSUUBBSSEEPP
  230.           separates multiple subscripts (default 034)
  231.  
  232.      AARRGGCC argument count, assignable
  233.  
  234.      AARRGGVV argument array, assignable; non-null members are taken
  235.           as filenames
  236.  
  237.      Functions may be defined (at the position of a pattern-
  238.      action statement) thus:
  239.  
  240.           function foo(a, b, c) { ...; return x }
  241.  
  242.      Parameters are passed by value if scalar and by reference if
  243.      array name; functions may be called recursively.  Parameters
  244.      are local to the function; all other variables are global.
  245.  
  246. EEXXAAMMPPLLEESS
  247.      length > 72
  248.  
  249.           Print lines longer than 72 characters.
  250.  
  251.      { print $2, $1 }
  252.  
  253.           Print first two fields in opposite order.
  254.  
  255.      BEGIN     { FS = ",[ \t]*|[ \t]+" }
  256.           { print $2, $1 }
  257.  
  258.  
  259.  
  260.  
  261. AT&T UNIX System Toolchest7 December 1987                        4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. AWK                       User Commands                       AWK
  269.  
  270.  
  271.  
  272.           Same, with input fields separated by comma and/or
  273.           blanks and tabs.
  274.  
  275.           { s += $1 }
  276.      END  { print "sum is", s, " average is", s/NR }
  277.  
  278.           Add up first column, print sum and average.
  279.  
  280.      /start/, /stop/
  281.  
  282.           Print all lines between start/stop pairs.
  283.  
  284.      BEGIN     {    # Simulate echo(1)
  285.           for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
  286.           printf "\n"
  287.           exit }
  288.  
  289. SSEEEE AALLSSOO
  290.      lex(1), sed(1)
  291.      A. V. Aho, B. W. Kernighan, P. J. Weinberger, _A_w_k - _a _P_a_t_-
  292.      _t_e_r_n _S_c_a_n_n_i_n_g _a_n_d _P_r_o_c_e_s_s_i_n_g _L_a_n_g_u_a_g_e: _U_s_e_r'_s _M_a_n_u_a_l
  293.  
  294. BBUUGGSS
  295.      There are no explicit conversions between numbers and
  296.      strings.  To force an expression to be treated as a number
  297.      add 0 to it; to force it to be treated as a string concaten-
  298.      ate "" to it.
  299.      The scope rules for variables in functions are a botch.
  300.      The (undocumented) options --SS and --RR are flaky.
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. AT&T UNIX System Toolchest7 December 1987                        5
  328.  
  329.  
  330.  
  331.